package com.duan.laugh.starter.minio.config;

import com.duan.laugh.starter.minio.core.MinioTemplate;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;

/**
 * @author duanjw
 */
@Configuration
@EnableConfigurationProperties({MinioProperties.class})
public class MinioAutoConfiguration {

    @Configuration
    @ConditionalOnMissingBean(MinioClient.class)
    protected static class MinioClientCreator {
        @Bean
        @SneakyThrows
        public MinioClient minioClient(MinioProperties minioProperties){
            return new MinioClient(minioProperties.getEndpoint(), minioProperties.getPort(), minioProperties.getAccessKey(),minioProperties.getSecretKey());
        }
    }

    @Configuration
    @Import({MinioAutoConfiguration.MinioClientCreator.class})
    protected static class MinioTemplateConfiguration{
        @Bean
        @ConditionalOnSingleCandidate(MinioClient.class)
        @ConditionalOnMissingBean
        public MinioTemplate minioTemplate(MinioClient minioClient) {
            return new MinioTemplate(minioClient);
        }
    }
//    @Bean
//    @Primary
//    @ConditionalOnMissingBean({MinioTemplate.class})
//    public MinioTemplate minioTemplate() {
//        return new MinioTemplate(minioClient);
//    }
}
